package com.gongguizhijia.Util;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.gongguizhijia.pojo.ECU;
import com.gongguizhijia.pojo.EcuInfoParseConfig;
import com.gongguizhijia.pojo.VersionInfoModel;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 导出Excel工具
 * @author：chj
 * @time：2021/6/9 9:33
 */

public class ExcelWriterUtil {
    /**
     * 获取数据并制表
     *
     * @param sheetName
     * @param filePath
     * @param clazz
     * @param modelsAll
     */
    public static void getNewExcelFile(String sheetName, String filePath, Class clazz, List<?> modelsAll) {

        ExcelWriter excelWriter = getExcelWriter(filePath);

        WriteSheet writeSheet = EasyExcel.writerSheet(1, sheetName).head(clazz).build();
        excelWriter.write(modelsAll, writeSheet);
        excelWriter.finish();
    }

    /**
     * 获取Excel输出流
     *
     * @param filePath
     * @return
     * @throws FileNotFoundException
     */
    public static ExcelWriter getExcelWriter(String filePath) {

        File file = new File(filePath);
        //如果已经存在就删除
        if (file.exists()) {
            file.delete();
        }

        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        ExcelWriter excelWriter = EasyExcelFactory.getWriter(outputStream);

        //字体
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontName("宋体");

        return excelWriter;
    }

    /**
     * 打印第一列
     *
     * @return
     */
    public static List<Object> getLineOne() {
        List<Object> list = new ArrayList<>();
        list.add("数据流PRI");
        list.add("PRI");
        list.add("协议");
        return list;
    }

    /**
     * 打印第二列
     *
     * @return
     */
    public static List<Object> getLineTwo() {
        List<Object> list = new ArrayList<>();
        list.add("");
        list.add("0");
        list.add("");
        return list;
    }

    /**
     * 打印第三列
     *
     * @return
     */
    public static List<Object> getLineThree() {
        List<Object> list = new ArrayList<>();
        list.add("");
        list.add("1");
        list.add("");
        return list;
    }

    /**
     * 从ecu中获取并填充versionInfoModel
     *
     * @param ecu
     * @return
     */
    public static VersionInfoModel getVersionInfoModel1(ECU ecu) {
        VersionInfoModel versionInfoModel = new VersionInfoModel();
        Integer StartPlace = null;
        Integer DataLength = null;
        String formula = null;
        String valueType = null;
        //版本信息默认解析方式为0-9
        String analysisType = "1-14";
        //处理解析公式
        //获取Start和length
        //StartIndex,DataLength
        if (StringUtils.isNotBlank(ecu.getStartIndex()) || StringUtils.isNotBlank(ecu.getDataLength())) {
            if (StringUtils.isBlank(ecu.getStartIndex())) {
                StartPlace = 00;
            } else {
                StartPlace = Integer.valueOf(Utils.subNumberText(ecu.getStartIndex()));
                DataLength = Integer.valueOf(ecu.getDataLength());
                analysisType = "0-9";
            }
        }
        //StartIndexb,DataLengthb
        if (StringUtils.isNotBlank(ecu.getStartIndexb()) || StringUtils.isNotBlank(ecu.getDataLengthb())) {
            if (StringUtils.isBlank(ecu.getStartIndexb())) {
                StartPlace = 00;
            } else {
                StartPlace = Integer.valueOf(Utils.subNumberText(ecu.getStartIndex()));
                DataLength = Integer.valueOf(ecu.getDataLengthb());
                analysisType = "0-9";
            }
        }
        //BitStart,BitLength
        if (StringUtils.isNotBlank(ecu.getBitStart()) && StringUtils.isNotBlank(ecu.getBitLength())) {
            StartPlace = Integer.valueOf(Utils.subNumberText(ecu.getBitStart()));
            DataLength = Integer.valueOf(Utils.subNumberText(ecu.getBitLength()));
            //ByteStart,ByteLength
        } else if ("".equals(ecu.getByteStart()) && "".equals(ecu.getByteLength())) {
            Integer S = null;
            Integer D = null;
            S = Integer.valueOf(Utils.subNumberText(ecu.getByteStart()));
            D = Integer.valueOf(Utils.subNumberText(ecu.getByteLength()));
            StartPlace = S * 8;
            DataLength = D * 8;
        } else if (StringUtils.isBlank(ecu.getBitStart()) && StringUtils.isNotBlank(ecu.getBitLength())) {
            StartPlace = 00;
            DataLength = Integer.valueOf(Utils.subNumberText(ecu.getBitLength()));
        }
        //1.asc码，2bcd，3数值，
        if ("0-9".equals(analysisType)) {
            valueType = ecu.getValueType();
            switch (valueType) {
                case "0":
                    versionInfoModel.setDecode("0");
                    versionInfoModel.setAnalyticFormula("0");
                    break;
                case "1":
                    versionInfoModel.setDecode("RK002");
                    versionInfoModel.setAnalyticFormula(ArkProtocolRules.getFormulaByRK002(StartPlace, DataLength, ecu.getPid()));
                    break;
                case "2":
                    versionInfoModel.setDecode("RK001");
                    versionInfoModel.setAnalyticFormula(ArkProtocolRules.getFormulaByRK001(StartPlace, DataLength, ecu.getPid()));
                    break;
                case "3":
                    versionInfoModel.setDecode("RD001");
                    versionInfoModel.setAnalyticFormula(ArkProtocolRules.getFormulaByRD001(StartPlace, DataLength, ecu.getPid()));
                    break;
                case "4":
                    versionInfoModel.setDecode("4");
                    versionInfoModel.setAnalyticFormula("4");
                    break;
                case "5":
                    versionInfoModel.setDecode("5");
                    versionInfoModel.setAnalyticFormula("5");
                    break;
                case "6":
                    versionInfoModel.setDecode("6");
                    versionInfoModel.setAnalyticFormula("6");
                    break;
                case "7":
                    versionInfoModel.setDecode("7");
                    versionInfoModel.setAnalyticFormula("7");
                    break;
                case "8":
                    versionInfoModel.setDecode("8");
                    versionInfoModel.setAnalyticFormula("8");
                    break;
                case "9":
                    versionInfoModel.setDecode("9");
                    versionInfoModel.setAnalyticFormula("9");
                    break;
                default:
                    versionInfoModel.setDecode("暂不支持的解析方式:" + ecu.getValueType());
                    String other = ArkProtocolRules.getForOtherProtocol(StartPlace, DataLength, ecu.getPid());
                    versionInfoModel.setAnalyticFormula("取位结果:" + other);
            }
        } else if ("1-14".equals(analysisType)) {
            valueType = ecu.getValueType();
            switch (valueType) {
                case "1":
                    versionInfoModel.setDecode("1");
                    versionInfoModel.setAnalyticFormula("1");
                    break;
                case "2":
                    versionInfoModel.setDecode("2");
                    versionInfoModel.setAnalyticFormula("2");
                    break;
                case "3":
                    versionInfoModel.setDecode("3");
                    versionInfoModel.setAnalyticFormula("3");
                    break;
                case "4":
                    versionInfoModel.setDecode("4");
                    versionInfoModel.setAnalyticFormula("4");
                    break;
                case "5":
                    versionInfoModel.setDecode("5");
                    versionInfoModel.setAnalyticFormula("5");
                    break;
                case "6":
                    versionInfoModel.setDecode("6");
                    versionInfoModel.setAnalyticFormula("6");
                    break;
                case "7":
                    versionInfoModel.setDecode("7");
                    versionInfoModel.setAnalyticFormula("7");
                    break;
                case "8":
                    versionInfoModel.setDecode("8");
                    versionInfoModel.setAnalyticFormula("8");
                    break;
                case "9":
                    versionInfoModel.setDecode("9");
                    versionInfoModel.setAnalyticFormula("9");
                    break;
                case "10":
                    versionInfoModel.setDecode("10");
                    versionInfoModel.setAnalyticFormula("10");
                    break;
                case "11":
                    versionInfoModel.setDecode("11");
                    versionInfoModel.setAnalyticFormula("11");
                    break;
                case "12":
                    versionInfoModel.setDecode("12");
                    versionInfoModel.setAnalyticFormula("12");
                    break;
                case "13":
                    versionInfoModel.setDecode("13");
                    versionInfoModel.setAnalyticFormula("13");
                    break;
                case "14":
                    versionInfoModel.setDecode("14");
                    versionInfoModel.setAnalyticFormula("14");
                    break;
                default:
                    versionInfoModel.setDecode("暂不支持的解析方式:" + ecu.getValueType());
                    String other = ArkProtocolRules.getForOtherProtocol(StartPlace, DataLength, ecu.getPid());
                    versionInfoModel.setAnalyticFormula("取位结果:" + other);
            }
        }

        //构建表格对象
        versionInfoModel.setKvId("");
        versionInfoModel.setDesc(ecu.getLang0());
        versionInfoModel.setOrder(Utils.getFileAddSpace(ecu.getPid()));
        versionInfoModel.setUnit("");
        versionInfoModel.setIsShow("是");
        versionInfoModel.setRemark("解放");

        return versionInfoModel;
    }

    /**
     * 从ecuInfoParseConfig中获取并填充versionInfoModel
     *
     * @param ecuInfoParseConfig
     * @return
     */
    public static VersionInfoModel getVersionInfoModel2(EcuInfoParseConfig ecuInfoParseConfig) {
        VersionInfoModel versionInfoModel = new VersionInfoModel();
        Integer StartPlace;
        Integer DataLength;
        String formula = null;
        Integer S = null;
        Integer D = null;
        if (StringUtils.isBlank(ecuInfoParseConfig.getStartByteIndex())) {
            S = 0;
            D = Integer.valueOf(Utils.subNumberText(ecuInfoParseConfig.getDataLength()));
            StartPlace = S;
            DataLength = D;
        } else {
            S = Integer.valueOf(Utils.subNumberText(ecuInfoParseConfig.getStartByteIndex()));
            D = Integer.valueOf(Utils.subNumberText(ecuInfoParseConfig.getDataLength()));
            StartPlace = S * 8;
            DataLength = D * 8;
        }

        //获取Sid并判空
        String sid = ecuInfoParseConfig.getSId();

        if (StringUtils.isNotBlank(sid)) {
            sid = Utils.getFileAddSpace(sid);
        }
        //ASCII,BCD,HEX,Int,BCD(1-4) ASCII(5-12)
        if ("ASCII".equals(ecuInfoParseConfig.getParseType())) {
            versionInfoModel.setDecode("RK002");
            versionInfoModel.setAnalyticFormula(ArkProtocolRules.getFormulaByRK002(StartPlace, DataLength, sid));
        } else if ("BCD".equals(ecuInfoParseConfig.getParseType()) || "HEX".equals(ecuInfoParseConfig.getParseType())) {
            versionInfoModel.setDecode("RK001");
            versionInfoModel.setAnalyticFormula(ArkProtocolRules.getFormulaByRK001(StartPlace, DataLength, sid));
        } else {
            versionInfoModel.setDecode("暂不支持的解析方式:" + ecuInfoParseConfig.getParseType());
        }

        //构建表格对象
        versionInfoModel.setKvId("");
        versionInfoModel.setDesc(ecuInfoParseConfig.getParaName());
        versionInfoModel.setOrder(Utils.getFileAddSpace(sid));
        versionInfoModel.setUnit("");
        versionInfoModel.setIsShow("是");
        versionInfoModel.setRemark("解放");

        return versionInfoModel;
    }
}
